package com.freewater.springboot.demo.aop.api.composite;

import com.freewater.springboot.demo.aop.api.SimpleBeforeAdvice;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.math.BigDecimal;

public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        //main.testComposable1();
        main.testComposable2();
    }

    public void testComposable1() {
        final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        // 创建ComposablePointcut 控制流对象并添加AddMethodMatcher实例
        ComposablePointcut pointcut = new ComposablePointcut(ClassFilter.TRUE, new MethodMatchers.AddMethodMatcher());
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleBeforeAdvice());
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvisor(advisor);
        proxyFactory.setTarget(applicationContext.getBean(ProductService.class));
        ProductService proxy = (ProductService) proxyFactory.getProxy();
        // union FindMethodMatcher
        pointcut.union(new MethodMatchers.FindMethodMatcher());
        // 添加DeleteMethodMatcher
        // pointcut.intersection(new DeleteMethodMatcher());
        proxy.addProduct(new Product("iphone11", "河南郑州", new BigDecimal(5700), "电子产品", "apple"));
        proxy.addProduct(new Product("小米", "广东深圳", new BigDecimal(3200), "电子产品", "xiaomi"));
        System.out.println(proxy.findAllProduct());
        System.out.println(proxy.findProductByProductCompany("apple"));
        System.out.println(proxy.findProductByProductName("iphone11"));
    }

    public void testComposable2() {
        final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        // 创建ComposablePointcut 控制流对象并添加AddMethodMatcher实例
        ComposablePointcut pointcut = new ComposablePointcut(ClassFilter.TRUE, new MethodMatchers.AddMethodMatcher());
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleBeforeAdvice());
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvisor(advisor);
        proxyFactory.setTarget(applicationContext.getBean(ProductService.class));
        ProductService proxy = (ProductService) proxyFactory.getProxy();
        // union FindMethodMatcher
        pointcut.union(new MethodMatchers.FindMethodMatcher());
        // 添加DeleteMethodMatcher
        pointcut.intersection(new MethodMatchers.DeleteMethodMatcher());
        proxy.addProduct(new Product("iphone11", "河南郑州", new BigDecimal(5700), "电子产品", "apple"));
        proxy.addProduct(new Product("小米", "广东深圳", new BigDecimal(3200), "电子产品", "xiaomi"));
        System.out.println(proxy.findAllProduct());
        System.out.println(proxy.findProductByProductCompany("apple"));
        System.out.println(proxy.findProductByProductName("iphone11"));
    }
}
